package com.tro.mail.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.socket.TextMessage;

import com.tro.common.util.date.DateUtil;
import com.tro.common.util.easyui.Pagination;
import com.tro.common.util.file.FileUtils;
import com.tro.common.util.mybatis.Page;
import com.tro.common.util.office.OfficeUtil;
import com.tro.common.util.string.StringUtlis;
import com.tro.mail.dao.entity.MailAnnex;
import com.tro.mail.dao.entity.MailAnnexExample;
import com.tro.mail.dao.entity.MailForder;
import com.tro.mail.dao.entity.MailForderExample;
import com.tro.mail.dao.entity.MailInbox;
import com.tro.mail.dao.entity.MailInboxExample;
import com.tro.mail.dao.entity.MailMain;
import com.tro.mail.dao.entity.MailMarker;
import com.tro.mail.dao.entity.MailMarkerExample;
import com.tro.mail.dao.mapper.MailMarkMailMapper;
import com.tro.mail.service.MailAnnexManager;
import com.tro.mail.service.MailForderManager;
import com.tro.mail.service.MailInboxManager;
import com.tro.mail.service.MailMainManager;
import com.tro.mail.service.MailMarkerManager;
import com.tro.mail.service.MailScoketHandler;
import com.tro.mail.util.MailStatus;
import com.tro.mail.vo.InBoxVO;
import com.tro.mail.vo.InBoxVOSearch;

@Controller
@RequestMapping("/mail")
public class MailController {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	MailAnnexManager annexManager;

	@RequestMapping(value = "writMail")
	public String index(Model model) {
		model.addAttribute("bean", new MailMain());
		return "/mail/writMail";
	}

	@RequestMapping(value = "emailSeting")
	public String emailSeting(Model model) {

		return "/mail/emailSeting";
	}

	@RequestMapping(value = "sent")
	public String sent(HttpServletRequest request, Model model) {
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
		MailMarkerExample example = new MailMarkerExample();
		example.createCriteria().andOwerUserEqualTo(curretUser);
		List<MailMarker> list = markManager.selectByExample(example);
		// FORDER
		MailForderExample search = new MailForderExample();
		search.createCriteria().andForderCreateIdEqualTo(curretUser);
		List<MailForder> forders = forderManager.selectByExample(search);
		model.addAttribute("TAGS", list);
		model.addAttribute("FORDERS", forders);
		return "/mail/sentList";
	}

	@RequestMapping(value = "inBox")
	public String inBox(HttpServletRequest request, Model model) {
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
		MailMarkerExample example = new MailMarkerExample();
		example.createCriteria().andOwerUserEqualTo(curretUser);
		List<MailMarker> list = markManager.selectByExample(example);
		// FORDER
		MailForderExample search = new MailForderExample();
		search.createCriteria().andForderCreateIdEqualTo(curretUser);
		List<MailForder> forders = forderManager.selectByExample(search);
		model.addAttribute("TAGS", list);
		model.addAttribute("FORDERS", forders);
		return "/mail/inBoxList";
	}

	@Autowired
	MailMainManager mailManager;

	@RequestMapping(value = "save")
	public String save(MailMain mail, HttpServletRequest request, Model model) {
		// 避免依赖，直接从SESSION获取
		String createUserId = request.getSession().getAttribute("MAIL_USER_ID").toString();
		String createUserName = request.getSession().getAttribute("MAIL_USER_NAME").toString();
		mail.setCreateTime(new Date());
		mail.setCreateUserId(createUserId);
		mail.setCreateUserName(createUserName);
		mail.setForderId(MailStatus.SENT_ID);
		mailManager.sendMailN(mail);
		if (mail.getMailStatu().compareTo((short) 1) == 0) {
			model.addAttribute("msg", "已发送");
		} else {
			model.addAttribute("msg", "草稿箱");
		}
		model.addAttribute("id", mail.getId());
		return "/mail/Tips";
	}

	@RequestMapping(value = "toPer")
	public String toPer(Model model, String path, HttpServletRequest request) {
		model.addAttribute("filePath", path);
		Map<String, String> result = StringUtlis.fileName(path);
		String html = OfficeUtil.toHtml(result.get("fileName"),
				request.getSession().getServletContext().getRealPath("") + result.get("path"), "");
		model.addAttribute("html", html);
		return "/mail/docPreview";
	}

	@RequestMapping(value = "perView")
	@ResponseBody
	public String perview(String path, HttpServletRequest request) {
		Map<String, String> result = StringUtlis.fileName(path);
		String html = OfficeUtil.toHtml(result.get("fileName"),
				request.getSession().getServletContext().getRealPath("") + result.get("path"), "");
		return html;
	}

	@RequestMapping(value = "sentList")
	@ResponseBody
	public Pagination<MailMain> getList(MailMain model, HttpServletRequest request, String type) {
		String pageNum = request.getParameter("page");

		String rowsNum = request.getParameter("rows");

		if (!StringUtils.isNotBlank(pageNum)) {
			pageNum = "1";
		}
		if (!StringUtils.isNotBlank(rowsNum)) {
			rowsNum = "20";
		}
		if (!StringUtils.isNumeric(pageNum)) {
			pageNum = "0";
		}

		if (!StringUtils.isNumeric(rowsNum)) {
			rowsNum = "0";
		}
		Pagination<MailMain> page = new Pagination<MailMain>();
		InBoxVOSearch example = new InBoxVOSearch();
		Integer curPage = Integer.parseInt(pageNum);
		Integer prePage = curPage - 1;
		Integer row = Integer.parseInt(rowsNum);
		example.setPage(new Page(prePage * row, curPage * row));
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();

		try {
			if (StringUtils.isNotBlank(type)) {
				example.setMailStatus(MailStatus.MAIL_TEMP);
			}
			example.setCsid(curretUser);
			example.setForderId(2L);
			page.setPage(curPage);
			page.setSize(row);
			page.setRows(mailManager.getMails(example));
			page.setTotal(mailManager.getMailsCount(example));
		} catch (Exception e) {
			logger.error("获取发件箱列表失败", e);
		}

		return page;
	}

	@Autowired
	MailInboxManager inBoxManager;

	// 收件箱
	@RequestMapping(value = "inBoxList")
	@ResponseBody
	public Pagination<InBoxVO> getInBoxList(HttpServletRequest request, String type, String forderId) {
		String pageNum = request.getParameter("page");

		String rowsNum = request.getParameter("rows");

		if (!StringUtils.isNotBlank(pageNum)) {
			pageNum = "1";
		}
		if (!StringUtils.isNotBlank(rowsNum)) {
			rowsNum = "20";
		}
		if (!StringUtils.isNumeric(pageNum)) {
			pageNum = "0";
		}

		if (!StringUtils.isNumeric(rowsNum)) {
			rowsNum = "0";
		}
		Pagination<InBoxVO> page = new Pagination<InBoxVO>();
		InBoxVOSearch example = new InBoxVOSearch();
		Integer curPage = Integer.parseInt(pageNum);
		Integer prePage = curPage - 1;
		Integer row = Integer.parseInt(rowsNum);
		example.setPage(new Page(prePage * row, curPage * row));
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();

		try {
			example.setCsid(curretUser);
			if (StringUtils.isNotBlank(type) && type.equals("forder")) {
				if (StringUtils.isBlank(forderId)) {
					example.setForderId(-1L);
				} else {
					example.setForderId(Long.parseLong(forderId));
				}

			} else {
				example.setForderId(1L);
			}

			page.setPage(curPage);
			page.setSize(row);
			page.setRows(inBoxManager.selectForderMailList(example));
			page.setTotal(inBoxManager.selectForderMailCount(example));
		} catch (Exception e) {
			logger.error("获取收件箱列表失败", e);
		}

		return page;
	}

	@RequestMapping(value = "addPhoto")
	public String addPhoto() {
		return "/mail/imageUpload";
	}

	@RequestMapping(value = "addDoc")
	public String addDoc() {
		return "/mail/docUpload";
	}

	@RequestMapping(value = "addAnnex")
	public String addAnnex() {
		return "/mail/annexUpload";
	}

	@RequestMapping("uploadPicture")
	@ResponseBody
	public List<Map<String, String>> uploads(@RequestParam("file") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			return FileUtils.uploads(files, "", request.getSession().getServletContext().getRealPath(""));
		} catch (Exception e) {
			logger.error("上传失败", e);
		}
		List<Map<String, String>> error = new ArrayList<>();
		Map<String, String> re = new HashMap<>();
		re.put("isSuccess", "0");
		error.add(re);
		return error;
	}
	@Autowired
	MailScoketHandler handler;
	@RequestMapping("uploadDoc")
	@ResponseBody
	public List<Map<String, String>> uploadDoc(@RequestParam("file") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			List<Map<String, String>> result = FileUtils.uploads(files, "/static/doc",
					request.getSession().getServletContext().getRealPath(""));
			if (result != null && result.size() > 0) {
				String basePath = request.getSession().getServletContext().getRealPath("") + "/static/doc/";
				// 获取文件名
				String fileName = result.get(0).get("fileName");
				String imgPath = request.getSession().getServletContext().getRealPath("") + "/static/doc/image/";
				String content = OfficeUtil.toHtml(fileName, basePath, imgPath);
				content = StringUtlis.cleanHtml(content);
				result.get(0).put("fileUrl", content);
				result.get(0).put("isSuccess", "1");
			}
			return result;
		} catch (Exception e) {
			logger.error("上传失败", e);
		}
		List<Map<String, String>> error = new ArrayList<>();
		Map<String, String> re = new HashMap<>();
		re.put("isSuccess", "0");
		error.add(re);
		return error;
	}

	@RequestMapping(value = "view")
	public String mailView(String mailId, String id, String type, HttpServletRequest request, Model model,
			String backUrl) {
		if (StringUtils.isBlank(mailId)) {
			// 跳转到404页面
			return "";
		}
		boolean NEED_ANNEX = true;

		// 获取邮件实体
		MailMain bean = mailManager.selectByPrimaryKey(Long.parseLong(mailId));
		String userName = request.getSession().getAttribute("MAIL_USER_NAME").toString();
		String userId = request.getSession().getAttribute("MAIL_USER_ID").toString();

		if (userId.equals(bean.getCreateUserId())) {
			if (StringUtils.isBlank(type)) {
				model.addAttribute("IS_INBOX", 0);
			} else {
				// 自己发给自己的
				model.addAttribute("IS_INBOX", 1);
			}
		} else {
			model.addAttribute("IS_INBOX", 1);
			if (bean.getIsReceipt() != null && bean.getIsReceipt().compareTo(MailStatus.MAIL_NEED_RECEIPT) == 0) {
				// 需要回执 发送一封邮件给发送者
				MailMain receipt = new MailMain();
				receipt.setCreateTime(new Date());
				receipt.setCreateUserId(MailStatus.MAIL_SYS);
				receipt.setMailContent("用户" + userName + "，已打开邮件" + bean.getMailTitle());
				receipt.setMailTitle(bean.getMailTitle() + "<用户：" + userName + " 回执>");
				receipt.setMailStatu(MailStatus.MAIL_SYS_TAG);
				receipt.setConsigneeVal(bean.getCreateUserId());
				receipt.setCreateUserName("系统邮件");
				mailManager.sendMailN(receipt);
			}
			// 更新为已读；并查询是否为密送
			MailInboxExample search = new MailInboxExample();
			search.createCriteria().andMailIdEqualTo(Long.parseLong(mailId)).andOwerUserIdEqualTo(userId);
			List<MailInbox> inBoxs = inBoxManager.selectByExample(search);
			if (inBoxs == null || inBoxs.size() != 1) {
				return "";
			} else {
				MailInbox inBox = inBoxs.get(0);
				bean.setIsReceipt(inBox.getMailIsReceipt());
				if (inBox.getIsRead().compareTo((short) 1) != 0) {
					// 更新
					inBox.setIsRead(MailStatus.IS_READ);
					inBoxManager.updateByPrimaryKey(inBox);
				}
			}
			if (bean.getMailStatu().compareTo(MailStatus.MAIL_RECALL) == 0
					|| bean.getMailStatu().compareTo(MailStatus.MAIL_RECALL_DEL) == 0) {
				// 已撤回
				NEED_ANNEX = false;
				bean.setMailContent("<p>该邮件已被用户撤回</p>");
			}
		}

		// 处理下收件人、抄送人、发送人
		bean.setCcVal(StringUtlis.cleanMailUser(bean.getCcVal()));
		bean.setConsigneeVal(StringUtlis.cleanMailUser(bean.getConsigneeVal()));
		bean.setSecretVal(StringUtlis.cleanMailUser(bean.getSecretVal()));
		// 处理下时间
		bean.setDateTime(DateUtil.mailFormat(bean.getCreateTime()));
		// 加载附件列表
		if (NEED_ANNEX) {
			MailAnnexExample annexSearch = new MailAnnexExample();
			annexSearch.createCriteria().andMailIdEqualTo(bean.getId());
			List<MailAnnex> annexs = annexManager.selectByExample(annexSearch);
			bean.setAnnexs(annexs);
			bean.setAnnexCount(annexs.size());
		}
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
		MailMarkerExample example = new MailMarkerExample();
		example.createCriteria().andOwerUserEqualTo(curretUser);
		List<MailMarker> list = markManager.selectByExample(example);
		// FORDER
		MailForderExample search = new MailForderExample();
		search.createCriteria().andForderCreateIdEqualTo(curretUser);
		List<MailForder> forders = forderManager.selectByExample(search);
		model.addAttribute("TAGS", list);
		model.addAttribute("FORDERS", forders);
		// 当前邮件的tag
		if (StringUtils.isNotBlank(id)) {
			// 通过ID拿标记
			List<MailMarker> marks = markManager.selectMarkByRelation(Long.parseLong(id));
			bean.setMarkers(marks);
			// 通过ID 拿是否标星
			if (!id.equals(mailId)) {
				MailInbox inBox = inBoxManager.selectByPrimaryKey(Long.parseLong(id));
				bean.setIsStart(inBox.getIsStart() == null ? 0 : Short.parseShort(inBox.getIsStart().toString()));
			}
			bean.setMailId(Long.parseLong(id));
		}
		if (bean.getIsStart() == null) {
			bean.setIsStart((short) 0);
		}
		//// 获取未读收件箱邮件条数
		MailInboxExample inBoxExample = new MailInboxExample();
		inBoxExample.createCriteria().andOwerUserIdEqualTo(userId).andForderIdEqualTo(MailStatus.INBOX_ID)
				.andIsReadEqualTo(MailStatus.NOT_READ);
		Integer NOT_READ_COUNT = inBoxManager.countByExample(inBoxExample);
		// 通知
		try {
			handler.sendMessageToUser(userId, new TextMessage("NOT_READ:"+NOT_READ_COUNT));
		} catch (IOException e) {
			logger.error("推送消息失败");
		}
		model.addAttribute("bean", bean);
		model.addAttribute("back", backUrl);
		return "/mail/mailView";
	}

	/**
	 * 
	 * @Title: redo
	 * @Description: TODO(转发/再次编辑)
	 * @param @param
	 *            mailId
	 * @param @param
	 *            model
	 * @param @param
	 *            forward
	 * @param @return
	 *            设定文件
	 * @return String 返回类型
	 * @throws @author
	 *             leeko
	 */
	@RequestMapping(value = "redo")
	public String redo(String mailId, Model model, String forward) {
		if (StringUtils.isBlank(mailId)) {
			// 跳转到404页面
			return "";
		}
		// 获取邮件实体
		MailMain bean = mailManager.selectByPrimaryKey(Long.parseLong(mailId));
		// 加载附件列表
		MailAnnexExample annexSearch = new MailAnnexExample();
		annexSearch.createCriteria().andMailIdEqualTo(bean.getId());
		List<MailAnnex> annexs = annexManager.selectByExample(annexSearch);
		bean.setAnnexs(annexs);
		bean.setAnnexCount(annexs.size());
		if (StringUtils.isNotBlank(forward)) {
			bean.setCcTeam("");
			bean.setCcVal("");
			bean.setConsigneeTeam("");
			bean.setConsigneeVal("");
			bean.setSecretTeam("");
			bean.setSecretVal("");
		}
		model.addAttribute("bean", bean);
		return "/mail/writMail";
	}

	@RequestMapping(value = "del")
	public String del(String mailId, HttpServletRequest request, String callBack) {
		if (StringUtils.isBlank(mailId)) {
			return "";
		}
		String userName = request.getSession().getAttribute("MAIL_USER_ID").toString();
		MailInboxExample example = new MailInboxExample();
		example.createCriteria().andMailIdEqualTo(Long.parseLong(mailId)).andOwerUserIdEqualTo(userName);
		int tag = inBoxManager.deleteByExample(example);
		if (tag == 0) {
			// 更新数据
			mailManager.delMail(mailId);
		}
		if (StringUtils.isNotBlank(callBack)) {
			return "redirect:" + callBack;
		}
		return "redirect:/mail/sent";
	}

	@RequestMapping(value = "delInbox")
	public String delInbox(String mailId, HttpServletRequest request) {
		if (StringUtils.isBlank(mailId)) {
			return "";
		}
		String userName = request.getSession().getAttribute("MAIL_USER_ID").toString();
		MailInboxExample example = new MailInboxExample();
		example.createCriteria().andMailIdEqualTo(Long.parseLong(mailId)).andOwerUserIdEqualTo(userName);
		int tag = inBoxManager.deleteByExample(example);
		if (tag == 0) {
			// 更新数据
			mailManager.delMail(mailId);
		}
		return "redirect:/mail/inBox";
	}

	@RequestMapping(value = "reCall")
	@ResponseBody
	public String reCall(String mailId) {
		if (StringUtils.isBlank(mailId)) {
			return "error";
		}
		MailMain bean = mailManager.selectByPrimaryKey(Long.parseLong(mailId));
		if (bean != null) {
			Long diff = new Date().getTime() - bean.getCreateTime().getTime();
			if (diff <= MailStatus.MAX_RECALL) {
				bean.setMailStatu(MailStatus.MAIL_RECALL);
				mailManager.updateByPrimaryKeySelective(bean);
				return "success";
			} else {
				return "timeout";
			}
		}
		return "error";
	}

	@RequestMapping(value = "reply")
	public String reply(String mailId, Model model, String type, HttpServletRequest request) {
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
		if (StringUtils.isBlank(mailId)) {
			return "error";
		}
		MailMain bean = mailManager.selectByPrimaryKey(Long.parseLong(mailId));
		if (bean != null) {
			String toUser = bean.getCreateUserName() + "<" + bean.getCreateUserId() + ">;";
			if (StringUtils.isNotBlank(type) && type.equals("ALL")) {
				// 回复所有人
				String consigneeTeam = bean.getConsigneeTeam();
				String consigneeVal = bean.getConsigneeVal();
				String ccVal = bean.getCcVal();
				String ccTeam = bean.getCcTeam();
				consigneeTeam = StringUtlis.addressHandler(consigneeTeam, curretUser);
				consigneeVal = StringUtlis.addressHandler(consigneeVal, curretUser);
				ccVal = StringUtlis.addressHandler(ccVal, curretUser);
				ccTeam = StringUtlis.addressHandler(ccTeam, curretUser);
				bean.setCcTeam(ccTeam);
				bean.setCcVal(ccVal);
				bean.setConsigneeTeam(consigneeTeam);
				bean.setConsigneeVal(toUser + consigneeVal);
			} else {
				// 回复给发件人
				bean.setConsigneeVal(toUser);
				bean.setConsigneeTeam("");
				bean.setCcVal("");
				bean.setCcTeam("");
			}
			// 密送人不回复吧.....
			bean.setSecretVal("");
			bean.setSecretTeam("");
			model.addAttribute("bean", bean);
			return "/mail/writMail";
		}

		return "error";
	}

	@RequestMapping(value = "addTag")
	public String addTag(String ids, Model model) {
		model.addAttribute("ids", ids);
		return "/mail/addTag";
	}

	@Autowired
	MailMarkerManager markManager;

	@RequestMapping(value = "doTag")
	@ResponseBody
	public String doTag(MailMarker marker, Model model, HttpServletRequest request) {
		// 保存
		if (marker.getId() == null || marker.getId().compareTo(0L) == 0) {
			String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
			marker.setOwerUser(curretUser);
			markManager.insert(marker);
		} else {
			markManager.addMark(marker);
		}

		return "success_" + marker.getId();

	}

	@RequestMapping(value = "addForder")
	public String addForder(String ids, Model model) {
		model.addAttribute("ids", ids);
		return "/mail/addForder";
	}

	@Autowired
	MailForderManager forderManager;

	@RequestMapping(value = "doForder")
	@ResponseBody
	public String doForder(MailForder forder, Model model, HttpServletRequest request) {
		// 保存
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
		forder.setForderCreateId(curretUser);
		forderManager.insert(forder);
		return "success_" + forder.getId();

	}

	@RequestMapping("addStar")
	@ResponseBody
	public String addStar(String ids) {
		mailManager.addStar(ids);
		return "success";
	}

	@RequestMapping("unStar")
	@ResponseBody
	public String unStar(String ids) {
		mailManager.unStar(ids);
		return "success";
	}

	@Autowired
	MailMarkMailMapper mapper;

	@RequestMapping("delMark")
	@ResponseBody
	public String delMark(String id) {
		if (StringUtils.isBlank(id)) {
			return "ERROR";
		}
		mapper.deleteByPrimaryKey(Long.parseLong(id));
		return "success";
	}

	@RequestMapping("personalMessage")
	public String personalIndex() {

		return "/mail/personalMessage";
	}

	@RequestMapping("temp")
	public String temp(HttpServletRequest request, Model model) {
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
		MailMarkerExample example = new MailMarkerExample();
		example.createCriteria().andOwerUserEqualTo(curretUser);
		List<MailMarker> list = markManager.selectByExample(example);
		// FORDER
		MailForderExample search = new MailForderExample();
		search.createCriteria().andForderCreateIdEqualTo(curretUser);
		List<MailForder> forders = forderManager.selectByExample(search);
		model.addAttribute("TAGS", list);
		model.addAttribute("FORDERS", forders);
		return "/mail/tempList";
	}

	@RequestMapping("isStart")
	public String isStart(HttpServletRequest request, Model model) {
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
		MailMarkerExample example = new MailMarkerExample();
		example.createCriteria().andOwerUserEqualTo(curretUser);
		List<MailMarker> list = markManager.selectByExample(example);
		// FORDER
		MailForderExample search = new MailForderExample();
		search.createCriteria().andForderCreateIdEqualTo(curretUser);
		List<MailForder> forders = forderManager.selectByExample(search);
		model.addAttribute("TAGS", list);
		model.addAttribute("FORDERS", forders);
		return "/mail/starList";
	}

	@RequestMapping("startList")
	@ResponseBody
	public Pagination<InBoxVO> startList(HttpServletRequest request) {
		String pageNum = request.getParameter("page");

		String rowsNum = request.getParameter("rows");

		if (!StringUtils.isNotBlank(pageNum)) {
			pageNum = "1";
		}
		if (!StringUtils.isNotBlank(rowsNum)) {
			rowsNum = "20";
		}
		if (!StringUtils.isNumeric(pageNum)) {
			pageNum = "0";
		}

		if (!StringUtils.isNumeric(rowsNum)) {
			rowsNum = "0";
		}
		Pagination<InBoxVO> page = new Pagination<InBoxVO>();
		InBoxVOSearch example = new InBoxVOSearch();
		Integer curPage = Integer.parseInt(pageNum);
		Integer prePage = curPage - 1;
		Integer row = Integer.parseInt(rowsNum);
		example.setPage(new Page(prePage * row, curPage * row));
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();

		try {
			example.setCsid(curretUser);
			example.setIsStar((short) 1);
			page.setPage(curPage);
			page.setSize(row);
			page.setRows(inBoxManager.selectForderMailList(example));
			page.setTotal(inBoxManager.selectForderMailCount(example));
		} catch (Exception e) {
			logger.error("获取收件箱列表失败", e);
		}

		return page;
	}

	@RequestMapping(value = "forder")
	public String getForders(HttpServletRequest request, Model model) {
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
		MailMarkerExample example = new MailMarkerExample();
		example.createCriteria().andOwerUserEqualTo(curretUser);
		List<MailMarker> list = markManager.selectByExample(example);
		// FORDER
		MailForderExample search = new MailForderExample();
		search.createCriteria().andForderCreateIdEqualTo(curretUser);
		List<MailForder> forders = forderManager.selectByExample(search);
		model.addAttribute("TAGS", list);
		model.addAttribute("FORDERS", forders);
		return "/mail/forderList";
	}

	@RequestMapping(value = "getForder")
	@ResponseBody
	public Pagination<MailForder> getForder(HttpServletRequest request) {
		Pagination<MailForder> page = new Pagination<MailForder>();
		String curretUser = request.getSession().getAttribute("MAIL_USER_ID").toString();
		List<MailForder> forders = new ArrayList<>();
		MailForderExample search = new MailForderExample();
		search.createCriteria().andForderCreateIdEqualTo(curretUser);
		forders = forderManager.selectByExample(search);
		page.setRows(forders);
		page.setPage(1);
		page.setSize(1);
		return page;
	}
}
